package org.mybatis.table;

import org.jboss.logging.Logger;
import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl;

import java.util.StringTokenizer;


/**
 * MyBatis3 的实现
 */
public class TkMyBatis3Impl extends IntrospectedTableMyBatis3Impl {
    private Logger logger = Logger.getLogger(TkMyBatis3Impl.class);

    @Override
    protected void calculateJavaClientAttributes() {
        super.calculateJavaClientAttributes();
        String mapperName = this.calculateJavaClientInterfacePackage()
                + "." + this.generateDomainName() + "Dao";
        logger.debug("Rename MyBatis3JavaMapperTypeName:" + mapperName);
        super.setMyBatis3JavaMapperType(mapperName);
    }

    @Override
    public void calculateModelAttributes(){
        super.calculateModelAttributes();
        String packageName = super.calculateJavaModelPackage();
        StringBuilder sb = new StringBuilder();
        sb.append(packageName);
        sb.append('.');
        String newDomainName = this.generateDomainName() + "Entity";
        sb.append(newDomainName);
        logger.debug("Rename setBaseRecordType:" + newDomainName);
        this.setBaseRecordType(sb.toString());
    }

    @Override
    protected void calculateXmlAttributes() {
        super.calculateXmlAttributes();
        String xmlMapperName = this.generateDomainName() + "Mapper.xml";
        super.setMyBatis3XmlMapperFileName(xmlMapperName);
    }

    private String generateDomainName() {
        String newDomainName = this.fullyQualifiedTable.getIntrospectedTableName();
        StringTokenizer stringTokenizer = new StringTokenizer(newDomainName,"_");
        stringTokenizer.nextToken();
        StringBuilder sql = new StringBuilder();
        while (stringTokenizer.hasMoreTokens()) {
            char[] chars = stringTokenizer.nextToken().toCharArray();
            chars[0] = (char)(chars[0]- 32);
            sql.append(chars);
        }
        return sql.toString();
    }

    @Override
    public String getRemarks() {
        return super.getRemarks();
    }
}
